home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_qt.idb / usr / freeware / include / Qt / qimage.h.z / qimage.h
Encoding:
C/C++ Source or Header  |  1998-10-28  |  7.5 KB  |  275 lines

  1. /****************************************************************************
  2. ** $Id: qimage.h,v 2.29 1998/07/03 00:09:34 hanord Exp $
  3. **
  4. ** Definition of QImage and QImageIO classes
  5. **
  6. ** Created : 950207
  7. **
  8. ** Copyright (C) 1992-1998 Troll Tech AS.  All rights reserved.
  9. **
  10. ** This file is part of Qt Free Edition, version 1.40.
  11. **
  12. ** See the file LICENSE included in the distribution for the usage
  13. ** and distribution terms, or http://www.troll.no/free-license.html.
  14. **
  15. ** IMPORTANT NOTE: You may NOT copy this file or any part of it into
  16. ** your own programs or libraries.
  17. **
  18. ** Please see http://www.troll.no/pricing.html for information about 
  19. ** Qt Professional Edition, which is this same library but with a
  20. ** license which allows creation of commercial/proprietary software.
  21. **
  22. *****************************************************************************/
  23.  
  24. #ifndef QIMAGE_H
  25. #define QIMAGE_H
  26.  
  27. #ifndef QT_H
  28. #include "qpixmap.h"
  29. #include "qstrlist.h"
  30. #endif // QT_H
  31.  
  32.  
  33. class QImage
  34. {
  35. public:
  36.     enum Endian { IgnoreEndian, BigEndian, LittleEndian };
  37.  
  38.     QImage();
  39.     QImage( int width, int height, int depth, int numColors=0,
  40.         Endian bitOrder=IgnoreEndian );
  41.     QImage( const QSize&, int depth, int numColors=0,
  42.         Endian bitOrder=IgnoreEndian );
  43.     QImage( const char *fileName, const char *format=0 );
  44.     QImage( const char *xpm[] );
  45.     QImage( const QImage & );
  46.    ~QImage();
  47.  
  48.     QImage     &operator=( const QImage & );
  49.     QImage     &operator=( const QPixmap & );
  50.     void    detach();
  51.     QImage    copy()        const;
  52.     QImage    copy(int x, int y, int w, int h, int conversion_flags=0) const;
  53.     QImage    copy(QRect&)    const;
  54.  
  55.     bool    isNull()    const    { return data->bits == 0; }
  56.  
  57.     int        width()        const    { return data->w; }
  58.     int        height()    const    { return data->h; }
  59.     QSize    size()        const    { return QSize(data->w,data->h); }
  60.     QRect    rect()        const    { return QRect(0,0,data->w,data->h); }
  61.     int        depth()        const    { return data->d; }
  62.     int        numColors()    const    { return data->ncols; }
  63.     Endian     bitOrder()    const    { return (Endian) data->bitordr; }
  64.  
  65.     QRgb    color( int i )    const;
  66.     void    setColor( int i, QRgb c );
  67.     void    setNumColors( int );
  68.  
  69.     bool    hasAlphaBuffer() const;
  70.     void    setAlphaBuffer( bool );
  71.  
  72.     bool    allGray() const;
  73.     bool        isGrayscale() const;
  74.  
  75.     uchar      *bits()        const;
  76.     uchar      *scanLine( int ) const;
  77.     uchar     **jumpTable()    const;
  78.     QRgb       *colorTable()    const;
  79.     int        numBytes()    const;
  80.     int        bytesPerLine()    const;
  81.  
  82.     bool    create( int width, int height, int depth, int numColors=0,
  83.             Endian bitOrder=IgnoreEndian );
  84.     bool    create( const QSize&, int depth, int numColors=0,
  85.             Endian bitOrder=IgnoreEndian );
  86.     void    reset();
  87.  
  88.     void    fill( uint pixel );
  89.  
  90.     QImage    convertDepth( int ) const;
  91.     QImage    convertDepthWithPalette( int, QRgb* p, int pc, int cf=0 ) const;
  92.     QImage    convertDepth( int, int conversion_flags ) const;
  93.     QImage    convertBitOrder( Endian ) const;
  94.     QImage    smoothScale(int width, int height) const;
  95.  
  96. #if defined(HAS_BOOL_TYPE)
  97.     // Needed for binary compatibility - calls createAlphaMask(int)
  98.     QImage    createAlphaMask( bool dither=FALSE ) const;
  99. #else
  100.     // Needed for source compatibility - calls createAlphaMask(FALSE)
  101.     QImage    createAlphaMask() const;
  102. #endif
  103.  
  104.     QImage    createAlphaMask( int conversion_flags ) const;
  105.     QImage    createHeuristicMask( bool clipTight=TRUE ) const;
  106.  
  107.     static Endian systemBitOrder();
  108.     static Endian systemByteOrder();
  109.  
  110.     static const char *imageFormat( const char *fileName );
  111.     static QStrList inputFormats();
  112.     static QStrList outputFormats();
  113.  
  114.     bool    load( const char *fileName, const char *format=0 );
  115.     bool    loadFromData( const uchar *buf, uint len,
  116.                   const char *format=0 );
  117.     bool    loadFromData( QByteArray data, const char *format=0 );
  118.     bool    save( const char *fileName, const char *format ) const;
  119.  
  120.     bool    valid( int x, int y ) const;
  121.     int        pixelIndex( int x, int y ) const;
  122.     QRgb    pixel( int x, int y ) const;
  123.     void    setPixel( int x, int y, uint index_or_rgb );
  124.  
  125. private:
  126.     void    init();
  127.     void    freeBits();
  128.  
  129.     struct QImageData : public QShared {    // internal image data
  130.     int    w;                // image width
  131.     int    h;                // image height
  132.     int    d;                // image depth
  133.     int    ncols;                // number of colors
  134.     int    nbytes;                // number of bytes data
  135.     int    bitordr;            // bit order (1 bit depth)
  136.     QRgb   *ctbl;                // color table
  137.     uchar **bits;                // image data
  138.     bool    alpha;                // alpha buffer
  139.     } *data;
  140.  
  141.     friend void bitBlt( QImage* dst, int dx, int dy, const QImage* src,
  142.         int sx, int sy, int sw, int sh, int conversion_flags );
  143. };
  144.  
  145.  
  146. // QImage stream functions
  147.  
  148. QDataStream &operator<<( QDataStream &, const QImage & );
  149. QDataStream &operator>>( QDataStream &, QImage & );
  150.  
  151.  
  152. class QIODevice;
  153. typedef void (*image_io_handler)( QImageIO * ); // image IO handler
  154.  
  155.  
  156. class QImageIO
  157. {
  158. public:
  159.     QImageIO();
  160.     QImageIO( QIODevice     *ioDevice, const char *format );
  161.     QImageIO( const char *fileName, const char *format );
  162.    ~QImageIO();
  163.  
  164.  
  165.     const QImage &image()    const    { return im; }
  166.     int        status()    const    { return iostat; }
  167.     const char *format()    const    { return frmt; }
  168.     QIODevice  *ioDevice()    const    { return iodev; }
  169.     const char *fileName()    const    { return fname; }
  170.     const char *parameters()    const    { return params; }
  171.     const char *description()    const    { return descr; }
  172.  
  173.     void    setImage( const QImage & );
  174.     void    setStatus( int );
  175.     void    setFormat( const char * );
  176.     void    setIODevice( QIODevice * );
  177.     void    setFileName( const char * );
  178.     void    setParameters( const char * );
  179.     void    setDescription( const char * );
  180.  
  181.     bool    read();
  182.     bool    write();
  183.  
  184.     static const char *imageFormat( const char *fileName );
  185.     static const char *imageFormat( QIODevice * );
  186.     static QStrList inputFormats();
  187.     static QStrList outputFormats();
  188.  
  189.     static void defineIOHandler( const char *format,
  190.                  const char *header,
  191.                  const char *flags,
  192.                  image_io_handler read_image,
  193.                  image_io_handler write_image );
  194.  
  195. private:
  196.     QImage    im;                // image
  197.     int        iostat;                // IO status
  198.     QString    frmt;                // image format
  199.     QIODevice  *iodev;                // IO device
  200.     QString    fname;                // file name
  201.     char       *params;                // image parameters
  202.     char       *descr;                // image description
  203.  
  204. private:    // Disabled copy constructor and operator=
  205.     QImageIO( const QImageIO & );
  206.     QImageIO &operator=( const QImageIO & );
  207. };
  208.  
  209.  
  210. void bitBlt( QImage* dst, int dx, int dy, const QImage* src,
  211.         int sx=0, int sy=0, int sw=-1, int sh=-1, int conversion_flags=0 );
  212.  
  213.  
  214. /*****************************************************************************
  215.   QImage member functions
  216.  *****************************************************************************/
  217.  
  218. inline bool QImage::hasAlphaBuffer() const
  219. {
  220.     return data->alpha;
  221. }
  222.  
  223. inline uchar *QImage::bits() const
  224. {
  225.     return data->bits ? data->bits[0] : 0;
  226. }
  227.  
  228. inline uchar **QImage::jumpTable() const
  229. {
  230.     return data->bits;
  231. }
  232.  
  233. inline QRgb *QImage::colorTable() const
  234. {
  235.     return data->ctbl;
  236. }
  237.  
  238. inline int QImage::numBytes() const
  239. {
  240.     return data->nbytes;
  241. }
  242.  
  243. inline int QImage::bytesPerLine() const
  244. {
  245.     return data->h ? data->nbytes/data->h : 0;
  246. }
  247.  
  248. inline QImage QImage::copy(QRect& r) const
  249. {
  250.     return copy(r.x(), r.y(), r.width(), r.height());
  251. }
  252.  
  253. #if !(defined(QIMAGE_C) || defined(DEBUG))
  254.  
  255. inline QRgb QImage::color( int i ) const
  256. {
  257.     return data->ctbl ? data->ctbl[i] : (QRgb)-1;
  258. }
  259.  
  260. inline void QImage::setColor( int i, QRgb c )
  261. {
  262.     if ( data->ctbl )
  263.     data->ctbl[i] = c;
  264. }
  265.  
  266. inline uchar *QImage::scanLine( int i ) const
  267. {
  268.     return data->bits ? data->bits[i] : 0;
  269. }
  270.  
  271. #endif
  272.  
  273.  
  274. #endif // QIMAGE_H
  275.